<template>
<div class="factory_detail form_detail_page" v-loading="isLoading">
  <one-title :title="$route.query.id ? '工厂详情' : '工厂新增'"
             @refreshData="refresh"></one-title>
  <el-form ref="formList" @submit.native.prevent
           v-if="!isEmpty"
           :model="detailData"
           :rules="rules">
    <two-title title="基本信息"></two-title>
    <div class="from_panel">
      <div class="form_col">
        <el-form-item class="form_item" label="工厂编号:" prop="number">
          <div class="input_wapper">
            <el-input
              placeholder="请输入工厂编号"
              v-model.trim="detailData.number"
              clearable>
            </el-input>
          </div>
        </el-form-item>
        <el-form-item class="form_item" label="工厂名称:" prop="name">
          <div class="input_wapper">
            <el-input
              placeholder="请输入工厂名称"
              v-model.trim="detailData.name"
              clearable>
            </el-input>
          </div>
        </el-form-item>
      </div>
      <div class="form_col">
        <el-form-item class="form_item" label="工厂地址:" prop="address">
          <div class="input_wapper">
            <el-input
              placeholder="请输入工厂地址"
              v-model.trim="detailData.address"
              clearable>
            </el-input>
          </div>
        </el-form-item >
        <el-form-item class="form_item" label="工厂电话:" prop="phone">
          <div class="input_wapper">
            <el-input
              placeholder="请输入工厂电话"
              v-model.trim="detailData.phone"
              clearable>
            </el-input>
          </div>
        </el-form-item>
      </div>
    </div>
    <two-title title="其他信息"></two-title>
    <div class="from_panel">
      <div class="form_col">
        <el-form-item class="form_item" label="工厂传真:" prop="fax">
          <div class="input_wapper">
            <el-input
              placeholder="请输入工厂传真"
              v-model.trim="detailData.fax"
              clearable>
            </el-input>
          </div>
        </el-form-item>
        <el-form-item class="form_item" label="备注:" prop="remarks">
          <div class="input_wapper text_area">
            <el-input type="textarea"
                      :autosize="{ minRows: 2, maxRows: 4}"
                      placeholder="请输入备注"
                      v-model="detailData.remarks"
                      maxlength="255"></el-input>
            <span class="text_length">{{detailData.remarks ? detailData.remarks.length : 0}}/255</span>
          </div>
        </el-form-item>
        <el-form-item class="form_item" label=" ">
          <div class="input_wapper mes_btn">
            <el-button type="primary" native-type="submit" @click="save">保存</el-button>
            <el-button @click="cancel">取消</el-button>
          </div>
        </el-form-item>
      </div>
    </div>
  </el-form>
  <empty-page v-else-if="isEmpty&&!isLoading"></empty-page>
</div>
</template>

<script>
import TwoTitle from '@/components/TwoTitle'
import EmptyPage from '@/components/EmptyPage'

export default {
  name: 'FactoryDetail',
  data () {
    return {
      // 是否为空数据
      isEmpty: false,
      //  加载中
      isLoading: true,
      // 开始监听数据
      startWatch: false,
      // 表单验证规则
      rules: {
        number: [
          {required: true, message: '请输入工厂编号', trigger: 'blur'},
          {max: 20, message: '工厂编号长度不能超过20字符', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入工厂名称', trigger: 'blur'},
          {max: 100, message: '工厂名称长度不能超过100字符', trigger: 'blur'}
        ],
        address: [
          {max: 100, message: '工厂地址长度不能超过100字符', trigger: 'blur'}
        ],
        remarks: [
          {max: 255, message: '备注长度不能超过255字符', trigger: 'blur'}
        ]
      }
    }
  },
  components: {
    TwoTitle,
    EmptyPage
  },
  computed: {
    // 详情数据
    detailData() {
      return this.$store.state.factory.detailData
    },
    saveState() {
      return this.$store.state.saveState
    }
  },
  watch: {
    detailData: {
      handler() {
        if (this.startWatch) {
          this.$store.commit('SetHasDetailChange', true)
        }
      },
      deep: true
    }
  },
  mounted () {
    this.init()
  },
  methods: {
    // 更新方法
    refresh() {
      this.init()
    },
    // 初始化方法
    init() {
      this.isLoading = true
      this.startWatch = false
      this.isEmpty = false
      let id = this.$route.query.id
      let postData = {
        method: 'get',
        module: 'factory',
        state: 'detailData',
        url: 'a/factory/factory/get/' + id
      }
      if (!id) {
        postData.clean = true
      }
      this.$store.dispatch('saveData', postData).then((res) => {
        this.startWatch = true
        if (id && !res) { this.isEmpty = true }
      }).catch(() => {
        this.isEmpty = true
      }).finally(() => {
        this.isLoading = false
      })
    },
    // 保存
    save() {
      if (this.saveState === true) return
      this.$refs['formList'].validate((valid) => {
        if (valid) {
          this.$store.commit('SetSaveState', true)
          let postData = {
            method: 'post',
            url: 'a/factory/factory/save',
            data: this.detailData,
            noCommit: true
          }
          let $route = this.$route
          this.$store.dispatch('saveData', postData).then(() => {
            this.$message({
              message: '保存成功',
              type: 'success',
              duration: 500,
              center: true,
              onClose: () => {
                this.$store.commit('SetHasDetailChange', false)
                this.$store.dispatch('delCurrentViews', $route).then(() => {
                  this.$store.commit('SetSaveState', false)
                  this.$router.push('/factory')
                  this.$store.commit('SetRefresh', true)
                })
              }
            })
          }).catch(() => {
            this.$store.commit('SetSaveState', false)
          })
        } else {
          return false
        }
      })
    },
    // 取消
    cancel() {
      let $route = this.$route
      this.backToList().then(() => {
        this.$store.dispatch('delCurrentViews', $route).then(() => {
          this.$router.push('/factory')
        })
      })
    }
  }
}
</script>

<style lang="less">
  .factory_detail {
  }
</style>
